﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MediaDefinitionService.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the MediaDefinitionService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Services
{
  using System;
  using DocProcessingRepository.Interfaces;
  using Entities;
  using Exceptions;
  using ServiceInterfaces;

  public class MediaDefinitionService : IMediaDefinitionService
  {
    private readonly IMediaDefinitionRepository mediaDefinitionRepository;

    private readonly IUserService userService;

    private readonly IMediaDefinitionAuditService auditService;

    public MediaDefinitionService(IMediaDefinitionRepository mediaDefinitionRepository, IUserService userService, IMediaDefinitionAuditService auditService)
    {
      this.mediaDefinitionRepository = mediaDefinitionRepository;
      this.userService = userService;
      this.auditService = auditService;
    }

    public PagedResult<MediaDefinitionEntity> GetByManCoDoc(Int32 pageNumber, Int32 numberOfItems, Int32 manCoDocId)
    {
      try
      {
        return this.mediaDefinitionRepository.GetByManCoDoc(pageNumber, numberOfItems, manCoDocId);
      }
      catch (Exception e)
      {
        throw new DocProcessingException("Unable to get media definitions", e);
      }
    }

    public void UpdatePrintFlag(Int32 mediaDefinitionId, Boolean print)
    {
      try
      {
        this.mediaDefinitionRepository.UpdatePrintFlag(mediaDefinitionId, print);

        ApplicationUser user = this.userService.GetApplicationUser();
        this.auditService.AddAudit(
          mediaDefinitionId,
          user.Id,
          MediaDefinitionAudit.MediaDefinitionAuditType.PrintFlagUpdated,
          DateTime.Now,
          String.Format("Print flag set as '{0}'", print));
      }
      catch (Exception e)
      {
        throw new DocProcessingException("Unable to update the print flag", e);
      }
    }
  }
}
